package com.ruifu.doc.service.impl;


import com.baomidou.mybatisplus.extension.api.R;
import com.fhzncloud.cloud.common.security.service.CloudUser;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ruifu.common.config.CodeType;
import com.ruifu.common.po.DmDocMaster;
import com.ruifu.common.po.DmDocc;
import com.ruifu.common.utils.BeanConverter;
import com.ruifu.common.utils.ProjectCodeUtils;
import com.ruifu.common.utils.StrUtils;
import com.ruifu.common.vo.PageResultVO;
import com.ruifu.doc.dto.docc.DmDoccDTO;
import com.ruifu.doc.mapper.DmDocMasterMapper;
import com.ruifu.doc.mapper.DmDoccMapper;
import com.ruifu.doc.service.DmDoccService;
import com.ruifu.doc.service.DmFileService;
import com.ruifu.doc.vo.docc.DmDoccVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 文档类型实现类
 * @author li176
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class DmDoccServiceImpl implements DmDoccService {

    @Autowired(required = false)
    private DmDoccMapper dmDoccMapper;

    @Autowired(required = false)
    private DmDocMasterMapper dmDocMasterMapper;
    @Autowired
    private DmFileService dmFileService;

    @Override
    public void insert(DmDoccDTO dmDoccDTO, CloudUser cloudUser) throws Exception {
        //判断属性集是否为空
        if (dmDoccDTO.getDcPropSets() == null || "".equals(dmDoccDTO.getDcPropSets())) {
            dmDoccDTO.setDcPropSets("{}");
        }
        /*DTO转PO*/
        DmDocc dmDocc = BeanConverter.copyProperties(dmDoccDTO, DmDocc.class);
        /*大小写字符转换*/
        //生命周期转换
        dmDocc.setDcDoclfc(StrUtils.upperToLower(dmDocc.getDcDoclfc()));
        //属性集定义转换
        dmDocc.setDcPropSets(StrUtils.upperToLower(dmDocc.getDcPropSets()));
        //不为默认类型
        dmDocc.setDcDefault("0");
        //添加文档类型对象
        dmDoccMapper.insert(dmDocc, cloudUser);
    }

    @Override
    public void updateById(DmDoccDTO dmDoccDTO, CloudUser cloudUser) {
        //判断属性集是否为空
        if (dmDoccDTO.getDcPropSets() == null || "".equals(dmDoccDTO.getDcPropSets())) {
            dmDoccDTO.setDcPropSets("{}");
        }
        //查询PO
        DmDocc dmDocc = dmDoccMapper.findById(dmDoccDTO.getDcDoccId());
        /*DTO合并到PO*/
        BeanConverter.copyProperties(dmDoccDTO, dmDocc);
        /*大小写字符转换*/
        //生命周期转换
        dmDocc.setDcDoclfc(StrUtils.upperToLower(dmDocc.getDcDoclfc()));
        //属性集定义转换
        dmDocc.setDcPropSets(StrUtils.upperToLower(dmDocc.getDcPropSets()));
        //修改文档类型对象
        dmDoccMapper.update(dmDocc, cloudUser);
    }

    @Override
    public void delete(Integer[] dcDoccIds, CloudUser cloudUser) throws Exception {
        //遍历id集合
        for (Integer dcDoccId : dcDoccIds) {
            //调用单个删除
            this.deleteById(dcDoccId, cloudUser);
        }
    }

    @Override
    public void deleteById(Integer dcDoccId, CloudUser cloudUser) throws Exception {
        //删除文档类型
        //根据id获取文档类型
        List<DmDocMaster> dcDoccIdList = dmDocMasterMapper.findByDcDoccId(dcDoccId);
        if (dcDoccIdList!=null && dcDoccIdList.size()!=0){
            throw new RuntimeException("该文档类型已被使用无法删除");
        }
        DmDocc dmDocc = dmDoccMapper.findById(dcDoccId);
        //修改删除标记
        dmDocc.setDcDelFlag(true);
        dmDoccMapper.update(dmDocc, cloudUser);
        //判断文档类型是否有含模板文件
        if (dmDocc.getDcTemplateId()!=null){
            //删除文件模板
            dmFileService.deleteFile(dmDocc.getDcTemplateId(), cloudUser);
        }
    }

    @Override
    public DmDoccVO findById(Integer dcDoccId) {
        //根据id查询结果返回
        DmDocc dmDocc = dmDoccMapper.findById(dcDoccId);
        /*PO转VO*/
        return BeanConverter.copyProperties(dmDocc, DmDoccVO.class);
    }

    @Override
    public Boolean findByDcDoccCode(String dcDoccCode, CloudUser cloudUser) {
        //根据类别名称查询类型记录
        return dmDoccMapper.findByDcDoccCode(dcDoccCode, cloudUser) == null;
    }

    @Override
    public PageResultVO findByQuery(int pageNum, int pageSize, DmDoccDTO dmDoccDTO, CloudUser cloudUser) {
        //开启分页组件
        PageHelper.startPage(pageNum, pageSize);
        //查询分页对象
        Page<DmDocc> page = (Page<DmDocc>) dmDoccMapper.findByQuery(dmDoccDTO, cloudUser);
        /*PO转VO*/
        List<DmDocc> dmDoccList = BeanConverter.copyList(page.getResult(), DmDocc.class);
        //返回分页结果
        return new PageResultVO(page.getTotal(), dmDoccList);
    }

    @Override
    public List<DmDoccVO> findAllDcDoccName(CloudUser cloudUser) {
        //查询所有项目类别
        List<DmDocc> dmDoccList = dmDoccMapper.findAll(cloudUser);
        //转换vo集合并返回
        return BeanConverter.copyList(dmDoccList, DmDoccVO.class);
    }

    /**
     * 获取文档类型编码
     * @return
     */
    @Override
    public String getDocTypeCode() {
        return ProjectCodeUtils.getCode(CodeType.DOCUMENT_TYPE);
    }

    /**
     * 新增默认文档类型
     * @param dcDoccId
     * @param cloudUser
     * @return
     */
    @Override
    public R insertDefaultType(Integer dcDoccId,CloudUser cloudUser) {
        int count = dmDoccMapper.selectDocTypeCount(cloudUser);
        if (count!=0){
            return R.failed("默认文档类型已存在");
        }
        try {
            DmDocc dmDocc = dmDoccMapper.findById(dcDoccId);
            dmDocc.setDcDefault("1");
            dmDoccMapper.update(dmDocc,cloudUser);
            return R.ok("新建成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.failed("新建失败");
    }

    /**
     * 取消默认文档类型
     * @param dcDoccId
     * @param cloudUser
     * @return
     */
    @Override
    public R updateDefaultType(Integer dcDoccId, CloudUser cloudUser) {
        try {
            DmDocc dmDocc = dmDoccMapper.findById(dcDoccId);
            dmDocc.setDcDefault("0");
            dmDoccMapper.update(dmDocc,cloudUser);
            return R.ok("取消成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.failed("取消失败");
    }
}
